/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 月
    /// </summary>
    [Serializable()]
    public class CMonth {
        /// <summary>
        /// 创建月
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        public CMonth(int year, int month) {
            m_year = year;
            m_month = month;
            createDays();
        }

        private HashMap<int, CDay> m_days = new HashMap<int, CDay>();

        /// <summary>
        /// 获取或设置日的集合
        /// </summary>
        public virtual HashMap<int, CDay> getDays() {
            return m_days;
        }

        /// <summary>
        /// 获取月的日数
        /// </summary>
        public virtual int getDaysInMonth() {
            return getDaysInMonth(m_year, m_month);
        }

        /// <summary>
        /// 获取该月的第一日
        /// </summary>
        public virtual CDay getFirstDay() {
            return m_days.get(1);
        }

        /// <summary>
        /// 获取该月的最后一日
        /// </summary>
        public virtual CDay getLastDay() {
            return m_days.get(m_days.size());
        }

        private int m_month;

        /// <summary>
        /// 获取月
        /// </summary>
        public virtual int getMonth() {
            return m_month;
        }

        private int m_year;

        /// <summary>
        /// 获取年
        /// </summary>
        public virtual int getYear() {
            return m_year;
        }

        /// <summary>
        /// 创建日的集合
        /// </summary>
        private void createDays() {
            int daysInMonth = getDaysInMonth();
            for (int i = 1; i <= daysInMonth; i++) {
                m_days.put(i, new CDay(m_year, m_month, i));
            }
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void delete() {
            foreach (CDay day in m_days.Values) {
                day.delete();
            }
            m_days.clear();
        }
        /// <summary>
        /// 获取一个月中有几天
        /// </summary>
        public int getDaysInMonth(int y, int m) {
            int d;
            int[] day = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            if (2 == m) {
                d = (((0 == y % 4) && (0 != y % 100) || (0 == y % 400)) ? 29 : 28);
            } else {
                d = day[m - 1];
            }
            return d;
        }
    }
}
